﻿using KinonekoSoftware.CodariaDB.Documents.Shenanigans.Forum;
using KinonekoSoftware.Studio.Models.Shenanigans.Forums;
using KinonekoSoftware.Studio.Pages.Shenanigans.Previewer;
using PlainTextMSG = KinonekoSoftware.Studio.Models.Shenanigans.Forums.PlainTextMSG;
using ImageMSG = KinonekoSoftware.Studio.Models.Shenanigans.Forums.ImageMSG;

namespace KinonekoSoftware.Studio.Pages.Shenanigans
{
    partial class ForumEditorViewModel : IObjectBuilder<ForumObject, ForumMSG>
    {
        private const           int      PerPageCount = 25;

        private readonly Dictionary<string, ForumMSG> _ForumMessageIDMap;
        private readonly List<ForumMSG>               _ForumMessageCollection;


        private int                  _pageIndex;
        private int                  _pageCount;
        private ForumMSGCreationFlag _msgCreationFlag;

        public ForumMSG Build(ForumMSGCreationFlag flag, ForumObject reference = null)
        {
            return ForumMSGBuilder.Build(_characterDataSource, _ForumMessageIDMap, flag, _poster.Id, reference);
        }

        public ForumMSG Build(ForumObject source)
        {
            return ForumMSGBuilder.Build(_characterDataSource, _ForumMessageIDMap, source);
        }

        public ForumMSG BuildAndTrack(ForumObject source)
        {
            var msg = Build(source);

            //
            //
            Track(msg);

            //
            //vv
            return msg;
        }
        
        private void Track(ForumMSG source)
        {

            //
            //
            if (source is not null)
            {
                _ForumMessageIDMap.TryAdd(source.Id, source);
                _ForumMessageCollection.Add(source);
                
                //
                // 添加到数据当中
                _discuss.Threads.Add(source.Target);
            }
        }

        private void Page()
        {
            //
            //
            _pageCount = (_ForumMessageCollection.Count + PerPageCount - 1) / PerPageCount;
            _pageIndex = 1;

            //
            //
            Page(_pageIndex);
        }

        private void Page(int index)
        {
            index      = Math.Clamp(index, 1, 255);
            _pageIndex = index;
            
            var offset = (index - 1) * PerPageCount;
            var remain = Math.Clamp(_ForumMessageCollection.Count - offset, 0, PerPageCount);
            
            //
            //
            PagedMessages.AddMany(_ForumMessageCollection.Skip(offset).Take(remain), true);
            
            //
            //
            RaiseUpdated(nameof(PageIndex));
            RaiseUpdated(nameof(PageCount));
        }

        private void DoPreviousPageCommand()
        {
            if (PageCount < 2 || PageIndex < 1)
            {
                return;
            } 
            
            Page(PageIndex - 1);
        }

        private void DoNextPageCommand()
        {
            if (PageCount <= 0 || PageIndex >= PageCount)
            {
                return;
            }
            
            Page(PageIndex + 1);
        }
        

        public int        MessageCount        => _ForumMessageCollection.Count;
        public ICommandEX PreviousPageCommand { get; }
        public ICommandEX NextPageCommand     { get; }

        public ObservableCollection<ForumMSG> PagedMessages { get; }

        public int PageCount
        {
            get => _pageCount;
            set => SetValue(ref _pageCount, value);
        }

        public int PageIndex
        {
            get => _pageIndex;
            set => SetValue(ref _pageIndex, value);
        }
    }
}